package com.example.leo.mycharmingstudent.mvp.mainpage;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.example.leo.mycharmingstudent.R;
import com.example.leo.mycharmingstudent.a0905.ToastUtil;
import com.example.leo.mycharmingstudent.a0917.practice.ArticleAdapter;
import com.example.leo.mycharmingstudent.a0917.practice.ArticleEntity;
import com.example.leo.mycharmingstudent.mvp.Injection;
import com.example.leo.mycharmingstudent.mvp.source.MVPTasksRepository;

import java.util.ArrayList;
import java.util.List;

public class MVPActivity extends AppCompatActivity implements MVPContract.View{

    private MVPContract.Presenter mPresenter;
    private RecyclerView rvArticle;
    private ArticleAdapter mArticleAdapter;
    private List<ArticleEntity.DataBean.DatasBean> mArticleList = new ArrayList<>();
    private Button btnArticleList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mvp);

        initView();
        initData();
        initEvent();

        mPresenter = new MVPPresenter(
                Injection.provideTasksRepository(getApplicationContext()),
                this);
    }

    private void initEvent() {
        btnArticleList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.showToast(MVPActivity.this,"假装这里有旋转等待框");
                mPresenter.articleList(mCurrentPage);
            }
        });
    }

    private void initView() {
        btnArticleList = findViewById(R.id.btn_article_list);
        rvArticle = findViewById(R.id.rv_article_project_fragment_main_page);

    }

    private void initData(){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rvArticle.setLayoutManager(linearLayoutManager);
        mArticleAdapter = new ArticleAdapter(mArticleList, this);
        rvArticle.setAdapter(mArticleAdapter);
    }

//    @Override
//    public void setPresenter(MVPContract.Presenter presenter) {
//    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.start();
    }

    int mCurrentPage = 0;

    @Override
    public void showArticleList(ArticleEntity articleEntity) {
        if (mCurrentPage == 0) {
            mArticleList.clear();
        }
        mArticleList.addAll(articleEntity.getData().getDatas());
        mArticleAdapter.notifyDataSetChanged();
    }

    @Override
    public void setPresenter(MVPContract.Presenter presenter) {

    }
}
